#include <stdio.h>
#include <string.h>

#include "dependecy.h"


int str_compare(const void *key1, const void *key2) {
    return strcmp((const char *)key1, (const char *)key2) == 0;
}

int int__compare(const void *key1, const void *key2) {
    return *(int *)key1 == *(int *)key2;
}

void hashmap_int_print(HashMap *map) {
    if (!map) return;
    for (size_t i = 0; i < map->capacity; i++) {
        printf("[%d] ", i);
        kvPeer *current = map->buckets[i];
        while (current) {
            printf("{%d->%d} ", *(int *)current->key, *(int *)current->value);
            current = current->next;
        }
        printf("\n");
    }
}

void hashmap_str_print(HashMap *map) {
    if (!map) return;
    for (size_t i = 0; i < map->capacity; i++) {
        printf("[%d] ", i);
        kvPeer *current = map->buckets[i];
        while (current) {
            printf("{%s->%s} ", (char *)current->key, (char *)current->value);
            current = current->next;
        }
        printf("\n");
    }
}

int main() {
    // 示例1：整数键值对
    HashMap *intMap = hashmap_create(
        16, sizeof(int), sizeof(int), 
        int_hash, int__compare, NULL, NULL
    );
    
    int keys[] = {1, 2, 3, 4, 5};
    int values[] = {100, 200, 300, 400, 500};
    
    for (int i = 0; i < 5; i++) {
        hashmap_put(intMap, &keys[i], &values[i]);
    }
    hashmap_int_print(intMap);
    int key = 3;
    int *val = (int *)hashmap_get(intMap, &key);
    if (val) printf("Value for %d: %d\n", key, *val);
    
    hashmap_destroy(intMap);
    
    // 示例2：字符串键值对
    HashMap *strMap = hashmap_create(
        23, 0, 0,  // 0表示直接使用指针
        string_hash, str_compare, NULL, NULL
    );
    
    hashmap_put(strMap, "nc.service", "/bin/nc");
    hashmap_put(strMap, "ls.service", "/bin/ls");
    hashmap_put(strMap, "ps.service", "/bin/ps");
    hashmap_put(strMap, "ping.service", "/bin/ping");
    hashmap_put(strMap, "ifconfig.service", "/sbin/ifconfig");
    hashmap_put(strMap, "route.service", "/sbin/route");
    hashmap_put(strMap, "netstat.service", "/bin/netstat");
    hashmap_put(strMap, "whoami.service", "/usr/bin/whoami");
    hashmap_put(strMap, "date.service", "/bin/date");
    hashmap_put(strMap, "uptime.service", "/bin/uptime");
    hashmap_put(strMap, "free.service", "/usr/bin/free");
    hashmap_put(strMap, "df.service", "/bin/df");
    hashmap_put(strMap, "top.service", "/usr/bin/top");
    hashmap_put(strMap, "killall.service", "/bin/killall");
    hashmap_put(strMap, "shutdown.service", "/sbin/shutdown");
    hashmap_put(strMap, "reboot.service", "/sbin/reboot");
    
    printf("nc.service: %s\n", (char *)hashmap_get(strMap, "nc.service"));
    printf("ls.service: %s\n", (char *)hashmap_get(strMap, "ls.service"));
    hashmap_str_print(strMap);
    printf("llllll\n");
    // hashmap_remove(strMap, "ls.service");
   
   
    hashmap_str_print(strMap);
    
    hashmap_destroy(strMap);    
    return 0;
}
